package com.tanhua.server.controller;

import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.TanhuaService;
import com.tanhua.server.service.UserInfoService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {


    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private TanhuaService tanhuaService;

    /**
     * 获取用户信息
     *
     * @param userId
     * @param token
     * @return
     */
    @GetMapping
    public ResponseEntity users(Long userId, @RequestHeader("Authorization") String token) {
        if (userId == null) {
            //获取用户id
//            Integer id = (Integer) JwtUtils.getClaims(token).get("id");
//            userId = Long.valueOf(id);
            //从线程空间(threadLocal)获取user的数据
            userId = UserHolder.getUserId();
        }
        //根据用户id调用service方法

        UserInfoVo userInfoVo = userInfoService.findById(userId);

        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");
        userInfo.setId(Long.valueOf(id));
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询是否喜欢
     *
     * @return
     */
    @GetMapping("/{id}/alreadyLove")
    public ResponseEntity alreadyLove(@PathVariable("id") Long id) {
        Boolean b = tanhuaService.alreadyLove(id);
        return ResponseEntity.ok(b);
    }

    /**
     * 查询数据
     *
     * @return
     */
    @GetMapping("/counts")
    public ResponseEntity counts() {
        Map map = tanhuaService.counts();
        return ResponseEntity.ok(map);
    }

    /**
     * 1 互相关注
     * 2 我关注
     * 3 粉丝
     * 4 谁看过我
     * @param type
     * @return
     */
    @GetMapping("/friends/{type}")
    public PageResult friends(@PathVariable("type") Integer type,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pagesize,
                              String nickname) {
        PageResult pageResult = tanhuaService.friends(type, page, pagesize, nickname);
        return pageResult;
    }

    //粉丝喜欢
    @PostMapping("/fans/{id}")
    public ResponseEntity fansLove(@PathVariable("id") String id) {
        tanhuaService.lave(id);
        return ResponseEntity.ok(null);
    }

    //粉丝喜欢取消
    @DeleteMapping("/like/{id}")
    public ResponseEntity unFansLove(@PathVariable("id") String id) {
        tanhuaService.unlove(id);
        return ResponseEntity.ok(null);
    }
}
